package com.libraryManage.controller;

import com.libraryManage.commons.config.Const;
import com.libraryManage.commons.utils.ResponseEntity;
import com.libraryManage.commons.utils.SimpleResp;
import com.libraryManage.commons.utils.jwt.AccountProfile;
import com.libraryManage.entity.LoginLogDo;
import com.libraryManage.service.LoginService;
import com.libraryManage.vo.ManagerVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Api(tags={"登录接口"})
@RestController
@CrossOrigin
@RequestMapping("/manage/login")
public class LoginController {

    @Autowired
    LoginService loginService;

    @ApiOperation("管理员登录接口")
    @ApiResponses({
            @ApiResponse(code=0, message="成功"),
            @ApiResponse(code=500, message="系统异常"),
            @ApiResponse(code=999, message="未登录"),
            @ApiResponse(code=1000, message="数据有效性检查不通过"),
            @ApiResponse(code=1001, message="业务规则检查不通过")
    })
    @RequestMapping(value = "/in" , method = RequestMethod.POST)
    public ResponseEntity<AccountProfile> managerLogin(@Validated @RequestBody ManagerVo vo,
                                                       HttpServletResponse response,
                                                       HttpServletRequest request){

        return loginService.managerLogin(vo,response,request);

    }

    @ApiOperation("退出登录")
    @ApiResponses({
            @ApiResponse(code=0, message="成功"),
            @ApiResponse(code=500, message="系统异常"),
            @ApiResponse(code=999, message="未登录"),
            @ApiResponse(code=1000, message="数据有效性检查不通过"),
            @ApiResponse(code=1001, message="业务规则检查不通过")
    })
    @RequestMapping(value = "/out" , method = RequestMethod.GET)
    public SimpleResp logout(){
        SecurityUtils.getSubject().logout();
        return new SimpleResp(Const.STAT_SUCC,"以退出登录");
    }

    @ApiOperation("获取登录日志")
    @ApiResponses({
            @ApiResponse(code=0, message="成功"),
            @ApiResponse(code=500, message="系统异常"),
            @ApiResponse(code=999, message="未登录"),
            @ApiResponse(code=1000, message="数据有效性检查不通过"),
            @ApiResponse(code=1001, message="业务规则检查不通过")
    })
    @RequestMapping(value = "/log" , method = RequestMethod.POST)
    public ResponseEntity<List<LoginLogDo>> getLogData(@RequestBody LoginLogDo loginLogDo){
        return loginService.getLogData(loginLogDo);
    }

}
